package home.yadong.email;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;

public class EmailReader {

	public static void main(String args[]) {
		Properties props = System.getProperties();
		props.setProperty("mail.store.protocol", "imaps");
			try {
				Session session = Session.getDefaultInstance(props, null);
				Store store = session.getStore("imaps");
			//	Store store = session.getStore("pop3s");
				store.connect("imap.gmail.com", "yadong.zhu", "Y51019791d");
				System.out.println("store:"+store);

				Folder inbox = store.getFolder("Inbox");
				inbox.open(Folder.READ_ONLY);
				Message messages[] = inbox.getMessages();
				Message lastMsg = messages[messages.length-1];
				System.out.println(lastMsg.getSubject()+" \nsend date:"+lastMsg.getSentDate());
				for (Address address:lastMsg.getFrom()){
					System.out.println("From:"+address);
					
				}
				Multipart mp = null;
				try {
					mp = (Multipart)lastMsg.getContent();
					for (int i = 0; i < mp.getCount(); i++) {
						Part part = mp.getBodyPart(i);	
						String disposition = part.getDisposition();
						if ((disposition!=null) &&
								((disposition.equalsIgnoreCase(Part.ATTACHMENT)||
									(disposition.equalsIgnoreCase(Part.INLINE))))) {
							saveFile(part.getFileName(),part.getInputStream());
							
						}
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				/*
				if (messages!=null){
					int size = messages.length;
					System.out.println(messages[size-1].getSubject()+" \nsend date:"+messages[size-1].getSentDate());
					for (Address address:messages[size-1].getFrom()){
						System.out.println("From:"+address);
						
					}
				}*/
//				if (messages!=null){
//					for(Message message:messages) {
//						System.out.println(message.getSubject()+" send date:"+message.getSentDate());
//						System.out.println(message.getSubject()+" send date:"+message.getSentDate());
//					}
//				}
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
			System.exit(1);
		} catch (MessagingException e) {
			e.printStackTrace();
			System.exit(2);
		}

	}

	private static void saveFile(String fileName, InputStream inputStream) throws IOException {
		File file = new File("/volumes/HD2T/MyDoc/"+fileName);
		for (int i = 0; file.exists(); i++) {
			file = new File(fileName+i);
		}
		OutputStream out = new FileOutputStream(file);
		int read = 0;
		byte[] bytes = new byte[1024];
	 
		while ((read = inputStream.read(bytes)) != -1) {
			out.write(bytes, 0, read);
		}
	 
		inputStream.close();
		out.flush();
		out.close();
	 
		System.out.println("New file "+file.getName()+"created!");
		
	}

}